/**
 * 路径分析
 * @author cqb
 * @class OpenLayers.Control.BufferAnalysis
 * @extends OpenLayers.Control
 * @version 1.0
 * @requires OpenLayers/Control.js
 */

OpenLayers.Control.BufferAnalysis = OpenLayers.Class(OpenLayers.Control,{
	/**
	 * Property: type {String} 工具类型
	 */
	type : OpenLayers.Control.TYPE_TOOL,

	/**
	 * Property: styleMap OpenLayers.styleMap
	 */
	styleMap : null,
	
	/**
	 * 
	 */
	bufferType: "point",

	/**
	 * Property: title {String} 工具按钮提示文字
	 */
	title : "点缓冲分析",
	/**
	 * 代理地址
	 * @property proxy
	 * @type {String}
	 */
	proxy: null,
	/**
	 * 路径分析地址
	 * @property server
	 * @type {String}
	 */
	server: null,
	/**
	 * 中心点
	 * @property center
	 * @type {LonLat}
	 */
	center: null,
	/**
	 * 缓冲半径
	 * @property radius
	 * @type {Float}
	 */
	radius: null,
	
	/**
	 * 绘制的样式
	 * @property routeSymbolizers
	 * @type {Object}
	 */
	routeSymbolizers: {
		"Line" : {
			strokeWidth : 3,
			strokeOpacity : 1,
			strokeColor : "#F0987C",
			strokeDashstyle : "solid"
		}
	},
	/**
	 * 绘制矢量图层
	 * @property layer
	 * @type {Layer}
	 */
	layer: null,
	
	/**
	 * Method: initialize 初始化
	 */
	initialize : function(options) {
		OpenLayers.Control.prototype.initialize.apply(this,arguments);
		var style = new OpenLayers.Style();
		style.addRules([ new OpenLayers.Rule({
			symbolizer : this.routeSymbolizers
		}) ]);
		this.styleMap = new OpenLayers.StyleMap({
			"default" : style
		});
		
		if(this.bufferType == "point"){
		    this.title = "点缓冲分析";
		    this.displayClass = this.displayClass+"Point";
		}
		if(this.bufferType == "line"){
		    this.title = "线缓冲分析";
		    this.displayClass = this.displayClass+"Line";
		}
		if(this.bufferType == "polygon"){
		    this.title = "面缓冲分析";
		    this.displayClass = this.displayClass+"Polygon";
		}
	},

	/**
	 * Method: activate 控件被触发时设置鼠标手势
	 */
	activate : function() {
// 		if(this.layer){
// 			this.layer.removeAllFeatures();
// 		}
		OpenLayers.Control.prototype.activate.apply(this,arguments);
		// this.map.layerContainerDiv.style.cursor = "url("
				// + AT21.WebGIS.getAT21RootLocation()
				// + "img/PointQuery.cur),default";
	},
	
	deactivate: function(){
		OpenLayers.Control.prototype.deactivate.apply(this,arguments);
		
		// if(this.vectorlayer){
			// this.vectorlayer.removeAllFeatures();
			// this.map.removeLayer(this.vectorlayer);
			// this.vectorlayer = null;
		// }

	},

	/**
	 * Method: draw
	 */
	draw : function() {
	    if(this.bufferType == "point"){
    		this.handler = new OpenLayers.Handler.Click(this, {
    			click : this.bufferQuery
    		});
	    }
        if(this.bufferType == "line"){
            this.handler = new OpenLayers.Handler.Path(this, {
                done: this.bufferQuery
            });
        }
        if(this.bufferType == "polygon"){
            this.handler = new OpenLayers.Handler.Polygon(this, {
                done: this.bufferQuery
            });
        }
	},
	
	/**
	 * 开始查询 
	 * @method bufferQuery
	 * @param evt {OpenLayers.Event} 事件
	 */
	bufferQuery : function(evt) {
	    var wkt = null;
        if(this.bufferType == "point"){
            var lonlat = this.map.getLonLatFromViewPortPx(evt.xy);
            wkt = "POINT("+lonlat.lon+" "+lonlat.lat+")";
        }
        if(this.bufferType == "line" || this.bufferType == "polygon"){
            var format = new OpenLayers.Format.WKT();
		    wkt = format.extractGeometry(evt);
        }
        this.queryend(wkt);
        
        this.deactivate();
	},
	
	/**
	 * 查询结束
	 * @method queryend
	 * @param features {Array} 空间对象
	 */
	queryend: function(features){
		
	},
	
	CLASS_NAME : "OpenLayers.Control.BufferAnalysis"
});